<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/25
 * Time: 11:05
 */
namespace app\admin\controller;

use app\admin\model\Food;
use app\common\model\HotelModel;
use think\Request;
use think\Db;
use think\Response;
use app\common\model\GuideModel;
use app\common\model\FoodModel;
use app\common\model\TourModel;
use app\admin\model\Guide as Guide;
use app\common\model\TeamModel;
class Choucheng extends Common
{
    /*
   * 敏感词的管理
   */
    public function Choucheng()
    {

        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $count = Db::name('merchant_tc')->count();
            $data = Db::name('merchant_tc')
                ->limit($startrow, $pagesize)
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));

        } else {
            return $this->fetch();
        }

    }

    /*
     * 酒店抽成
     */
    public function hotel()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['hotel_name']) && $pagedata['hotel_name'] != '') {
                $where['hotel_name'] = array('like', '%' . $pagedata['hotel_name'] . '%');
            }

            $count = Db::name('hotel')->where('is_del', 0)->where('is_pass', 1)->count();
            $data = Db::name('hotel')
                ->limit($startrow, $pagesize)
                ->where('is_del', 0)->where('is_pass', 1)
                ->where($where)
                ->order('hotel_id', 'desc')
                ->select();
            if(count($data)>0){
                foreach ($data as $key => $value) {
                    $data[$key]['commission_rate'] = $value['commission_rate']/10;
                }
            }
            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
           $res =  Db::name('hotel')
                ->where('is_del', 0)->where('is_pass', 1)
                ->order('hotel_id', 'desc')
                ->select();
           $this->assign('res',$res);
           return view();
        }
    }

    /*
     * 修改抽成
     */
    public function editHotel()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'commission_rate' => $post['data']['commission_rate'] * 10,

            );


            $res = Db::name('hotel')->where('hotel_id', $post['data']['hotel_id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 景点抽成设置
     */
    public function view()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['tour_name']) && $pagedata['tour_name'] != '') {
                $where['tour_name'] = array('like', '%' . $pagedata['tour_name'] . '%');
            }

            $count = Db::name('tours')->where('is_del', 0)->where('is_pass', 1)->count();
            $data = Db::name('tours')
                ->limit($startrow, $pagesize)
                ->where('is_del', 0)->where('is_pass', 1)
                ->where($where)
                ->order('tour_id', 'desc')
                ->select();
             if(count($data)>0){
                foreach ($data as $key => $value) {
                    $data[$key]['commission_rate'] = $value['commission_rate']/10;
                }
            }
            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
            $res = Db::name('tours')
                ->where('is_del', 0)->where('is_pass', 1)
                ->select();
            $this->assign('res',$res);
            return view();
        }
    }

    /*
     * 修改景点抽成
     */
    public function editView()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'commission_rate' => $post['data']['commission_rate'] * 10,

            );


            $res = Db::name('tours')->where('tour_id', $post['data']['tour_id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 旅行社抽成比例
     */
    public function team()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['team_name']) && $pagedata['team_name'] != '') {
                $where['team_name'] = array('like', '%' . $pagedata['team_name'] . '%');
            }
            $count = Db::name('team')->where('is_del', 0)->where('is_pass', 1)->count();
            $data = Db::name('team')
                ->limit($startrow, $pagesize)
                ->where('is_del', 0)->where('is_pass', 1)
                ->where($where)
                ->order('team_id', 'desc')
                ->select();
            if(count($data)>0){
                foreach ($data as $key => $value) {
                    $data[$key]['commission_rate'] = $value['commission_rate']/10;
                }
            }
            ajax_return(config('code.success_show'), '请求成功', $data, $count);
        } else {
            $res = Db::name('team')
                ->where('is_del', 0)->where('is_pass', 1)
                ->select();
            $this->assign('res',$res);
            return view();
        }
    }

    public function editTeam()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'commission_rate' => $post['data']['commission_rate'] * 10,

            );


            $res = Db::name('team')->where('team_id', $post['data']['team_id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }
    /*
     * 修改敏感字
     */
    public function editMingan()
    {
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $saveData = array(
                'munber'=>$post['data']['munber'],

            );


            $res = Db::name('merchant_tc')->where('id',$post['data']['id'])->update($saveData);

            if ($res!==false){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }
}